var {
  http
} = require('../../utils/apiutils');

var baidu = require('../../utils/bdutils.js')
const app = getApp()

Page({
  data: {
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3', "demo-text-1", "demo-text-1"],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    sh: "280rpx",
    interval: 2000,
    duration: 500,
    current: 1,
    records: [],
    pages: 0,
    userInfo: null,
    showV: -1
  },
  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },

  onReachBottom() {

    if (this.data.current < this.data.pages) {
      this.data.current = this.data.current + 1
      this.getJoinTraining()

    } else {
      wx.showToast({
        title: '没有更多了',
        icon: "none"
      })
      return;
    }

    // var that = this;
    // setTimeout(function () {
    //   wx.hideLoading()
    //   var newArr = that.data.background.concat(["aaa", "bbb", "33333", "555", "777"]);
    //   that.setData({
    //     background: newArr
    //   })
    // }, 3000)
  },
  onclickCell: function (e) {
    if (!getApp().globalData.userInfo.image) {
      wx.showToast({
        title: '请先在我的设置个人头像',
        icon: "none"
      })
      return;
    }
    var item = e.currentTarget.dataset.item;
    var data = {
      id: item.id,
      name: item.name,
    }
    if (!item.speed) {
      item.speed = 0;
      console.log("------拦截判断--")
    }
    getApp().globalData.faceIdentification = item.faceIdentification;
    getApp().globalData.faceType = item.faceType;
    getApp().globalData.popup = item.popup;
    wx.navigateTo({
      url: '/pages/course/course?passType=' + item.passType + '&id=' + item.id + '&name=' + item.name + "&faceIdentification=" + item.faceIdentification + "&speed=" + item.speed + "&popup=" + item.popup,
      success: (result) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  onclickB: function () {
    // wx.navigateTo({
    //   url: '/pages/web/web',
    //   success: (result) => {},
    //   fail: (res) => {},
    //   complete: (res) => {},
    // })

  },
  //事件处理函数
  bindViewTap: function () {

  },

  getJoinTraining: function () {

    wx.showLoading({
      title: '获取班级列表中...',
    })
    var prams = {
      "current": this.data.current,
      "size": "10" //每页显示条数
    }
    var that = this;
    http.getJoinTraining(prams,
      function (res) {
        wx.hideLoading();
        // console.log("----参与的培训列表---", res);
        var arr = res.data.records;
        // arr.forEach(function(item){
        //   item.state = 0;
        // })

        if (res.data.current == 1) {
          if (arr.length == 0) {
            that.setData({
              showV: 0, //res.data.records,
            })
          } else {
            that.setData({
              showV: 1, //res.data.records,
            })
          }
          that.setData({
            records: arr, //res.data.records,
            current: res.data.current,
            pages: res.data.pages,
          })
        } else {
          that.setData({
            records: [...this.data.records, ...arr], //res.data.records,
            current: res.data.current,
            pages: res.data.pages,
          })
        }

      },
      function (err) {
        wx.hideLoading();
      })
  },
  weChatLogin: function (code) {
    var prams = {
      "code": code
    }


    var that = this;
    // wx.showLoading({
    //   mask: true
    // })
    http.weChatLogin(prams,
      function (res) {

        if (res.status == 201) {
          app.globalData.openId = res.data;
          that.setData({
            showV: 0, //res.data.records,
          })
        } else if (res.status == 200) {

          that.data.userInfo = res.data;
          app.globalData.userInfo = res.data;
          // console.log('头像头像',app.globalData.userInfo.image)
          app.globalData.token = res.data.token;
          app.globalData.isLogin = true;
          that.getJoinTraining();

          http.userInfo({}, function (res) {
              if (res.status == 200) {
                app.globalData.userInfo = res.data;
                // console.log('头像头像2',app.globalData.userInfo.image)
              }
            },
            function (err) {}
          )

        }
        wx.nextTick(_ => {
          // wx.hideLoading()
        })
      },
      function (err) {
        console.log(err)
      })
  },
  onclickSc: function () {
    if (this.data.userInfo && this.data.userInfo.telphone && this.data.userInfo.telphone.length == 11) {
      if (this.data.userInfo.idNumber && this.data.userInfo.idNumber.length > 10) {
        // 允许从相机和相册扫码
        var that = this;
        wx.scanCode({
          success(res) {
            console.log("------二维码识别结果---", res)
            if (res.result) {
              let a = JSON.parse(res.result);
              if (a.id && a.id != "") {
                that.getTrainDirectory(a.id);
              } else {
                wx.showToast({
                  title: '无效二维码',
                  icon: "none"
                })
              }
            } else {
              wx.showToast({
                title: '没有检测到信息',
                icon: "none"
              })
            }
            // console.log(res)
          },
          fail: function () {
            wx.showToast({
              title: '没有检测到二维码信息',
              icon: "none"
            })
          }
        })
      } else {
        wx.switchTab({
          url: '/pages/me/me',
        })
        wx.showToast({
          title: '请先实名认证，实名认证后学习',
          icon: "none",
        })
      }


    } else {
      wx.navigateTo({
        url: '/pages/login/login?type=4'
      })
      wx.showToast({
        title: '登录后学习',
        icon: "none",
      })
    }
  },
  getTrainDirectory: function (str) {
    var data = {
      trainId: str,
    }
    var that = this;
    wx.showLoading({
      title: '正在添加课程...',
    })
    http.getTrainDirectory(data,
      function (res) {
        wx.hideLoading()
        if (res.status == 200) {
          wx.showToast({
            title: '添加课程成功,您可以去学习了',
            icon: "none",
          })
          that.getJoinTraining();
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()

      })
    console.log("-----------", )
  },

  handleContact(e) {
    console.log('---客服触发事件---', e)
  },

  onShow: function () {
    // console.log('页面show', app.globalData.userInfo)
    if (app.globalData.userInfo) {
      this.getJoinTraining();
      this.data.userInfo = app.globalData.userInfo;
    }
  },


  // onLoad: function () {
  //   //  baidu.getoAuthToken()
  //   var that = this;
  //   wx.login({
  //     success(res) {
  //       if (res.code) {
  //         that.weChatLogin(res.code)
  //       } else {
  //         wx.showToast({
  //           title: '登陆失败',
  //           icon: "none"
  //         })
  //       }
  //     },
  //     fail(err) {
  //       wx.showToast({
  //         title: '获取code失败',
  //         icon: "none"
  //       })
  //     }
  //   })

  // },



  onLoad: function () {
    // baidu.getoAuthToken()
    var that = this;
    wx.showLoading({
      mask: true
    })

    if (app.globalData.employId && app.globalData.employId != '') {
      wx.login({
        success(res) {
          if (res.code) {
            wx.hideLoading()
            that.weChatLogin(res.code)
          } else {
            wx.hideLoading()
            wx.showToast({
              title: '登陆失败',
              icon: "none"
            })
          }
        },
        fail(err) {
          wx.hideLoading()
          wx.showToast({
            title: '获取code失败',
            icon: "none"
          })
        }
      })

    } else {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.employIdCallback = employId => {
        if (employId != '') {
          wx.login({
            success(res) {
              if (res.code) {
                wx.hideLoading()
                that.weChatLogin(res.code)
              } else {
                wx.hideLoading()
                wx.showToast({
                  title: '登陆失败',
                  icon: "none"
                })
              }
            },
            fail(err) {
              wx.hideLoading()
              wx.showToast({
                title: '获取code失败',
                icon: "none"
              })
            }

          })

        }
      }
    }

  },

})